import {
  queryDepartmentListPage,
  queryRoleList,
  storeSelectPage,
} from "@/api/System";
import { getDistInfo } from "@/api/Other";
export const departmentStore = {};
export const roleStore = {};
export const storeStore = {};
export const brandStore = {};

// 部门
export const getDepartment = async (id) => {
  if (!id) {
    return;
  }
  const department = departmentStore["department"];
  if (department) return department
  const promise = queryDepartmentListPage({
    page: { size: 99999, current: 1 },
  }).then((res) => res.records);
  departmentStore["department"] = promise;
  return promise;
};
// 角色
export const getRole = async (id) => {
  if (!id) {
    return;
  }
  const role = roleStore["role"];
  if (role) return role;
  const promise = queryRoleList({
    page: { size: 99999, current: 1 },
  }).then((res) => res.records);
  roleStore["role"] = promise;
  return promise;
};

// 门店
export const getStore = async (id) => {
  if (!id) {
    return;
  }
  const store = storeStore["store"];
  if (store) return store;
  const promise = storeSelectPage({
    page: { size: 99999, current: 1 },
  }).then((res) => res.records);
  storeStore["store"] = promise;
  return promise;
};

// 字典 默认品牌
export const getBrand = async (id, permissionType = 3) => {
  if (!id) {
    return;
  }
  const store = brandStore["brand" + permissionType];
  if (store) return store;
  const promise = getDistInfo({
    permissionType,
  }).then((res) => res[0].groupData);
  brandStore["brand" + permissionType] = promise;
  return promise;
};
